﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using Sinacor.Infra.UI.Security.Licensing;

namespace Sinacor.Infra.UI.Controls.SmartClient
{
	public enum SinacorMenuType
	{
		Root,
		Child,
		ZoomControl
	}

	public sealed class SinacorMenu : Menu
	{
		public static readonly DependencyProperty SelectedCompanyProperty = DependencyProperty.Register("SelectedCompany", typeof(string), typeof(SinacorMenu), new FrameworkPropertyMetadata(""));
		public static readonly DependencyProperty SelectedModuleProperty = DependencyProperty.Register("SelectedModule", typeof(string), typeof(SinacorMenu), new FrameworkPropertyMetadata(""));
		public static readonly DependencyProperty StateProperty = DependencyProperty.Register("State", typeof(SinacorMenuState), typeof(Menu), new FrameworkPropertyMetadata(SinacorMenuState.Floating));
        public static readonly DependencyProperty ModulePlusProperty = DependencyProperty.Register("ModulePlus", typeof(bool), typeof(SinacorWindowMenu), new FrameworkPropertyMetadata(false));


		/// <summary>
		/// Agrupa itens do menu
		/// </summary>
		/// <param name="items">Itens que serão adicionados à lista</param>
		/// <param name="level">Nivel em que o grupo se encontra</param>
		public SinacorMenu(BindingList<object> items, SinacorMenuType menuType)
		{
			this.VerticalAlignment = System.Windows.VerticalAlignment.Top;
			this.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;

			switch (menuType)
			{
				case SinacorMenuType.Child:
					break;
				case SinacorMenuType.Root:
					break;
				case SinacorMenuType.ZoomControl:
					this.SetResourceReference(ListView.TemplateProperty, "ControlMenuItemZoom");
					this.SetResourceReference(ListView.TemplateProperty, "ControlSubmenu");
					break;
			}

			this.ItemsSource = items;

            //Aplica o ícone do SinacorPlus de acordo com o Kernel instalado na máquina
            this.ModulePlus = LicensingManager.GetInstance().ValidateKernelPlus();                            
		}

		/// <summary>
		/// Define o nome da empresa selecionada
		/// </summary>
		public string SelectedCompany
		{
			get { return (string)GetValue(SelectedCompanyProperty); }
			set { SetValue(SelectedCompanyProperty, value); }
		}

        /// <summary>
        /// Define o Id da empresa selecionada
        /// </summary>
        public int SelectedCompanyId { get; set; }

		/// <summary>
		/// Define o nome do modulo selecionado
		/// </summary>
		public string SelectedModule
		{
			get { return (string)GetValue(SelectedModuleProperty); }
			set { SetValue(SelectedModuleProperty, value); }
		}

		/// <summary>
		/// Define o estado corrente do menu
		/// </summary>
		public SinacorMenuState State
		{
			get { return (SinacorMenuState)GetValue(StateProperty); }
			set
			{
				SetValue(StateProperty, value);
				if (StateChanged != null)
					StateChanged(new SinacorMenuStateChangedEventArgs(value));
			}
		}

		/// <summary>
		/// Delega o evento SinacorMenuStateChangedHandler
		/// </summary>
		public delegate void SinacorMenuStateChangedHandler(SinacorMenuStateChangedEventArgs e);

		/// <summary>
		/// Evento que é disparado quando o estado do menu é alterado
		/// </summary>
		public event SinacorMenuStateChangedHandler StateChanged;

        /// <summary>
        /// True se a janela pertence ao módulo mais
        /// </summary>
        public bool ModulePlus
        {
            get { return (bool)GetValue(ModulePlusProperty); }
            set { SetValue(ModulePlusProperty, value); }
        }
	}
}